Skip to content

Conversation

@hiworldwzj
Copy link
Collaborator

No description provided.

@gemini-code-assist
Copy link

Summary of Changes

Hello @hiworldwzj, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on improving the robustness and observability of the inference batching process. It ensures that all CUDA operations related to adding requests are explicitly synchronized, which can prevent potential race conditions or data inconsistencies. Additionally, it provides a new profiling utility to help developers analyze and optimize the performance of CUDA and CPU operations within the system.

Highlights

  • CUDA Synchronization: Added a torch.cuda.current_stream().synchronize() call within the add_reqs function in infer_batch.py to ensure all CUDA operations are completed before the function returns, enhancing execution predictability.
  • Profiling Utility: Introduced a new script, profile_demo.py, which utilizes torch.profiler to demonstrate how to profile CPU and CUDA activities, saving traces for TensorBoard analysis.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a profiling script and adds a CUDA synchronization point. My review focuses on improving the robustness of the new script and discussing the performance implications of the added synchronization, suggesting a more efficient alternative if applicable.

slave_req: InferReq = slave_req
slave_req.related_master_req = master_req

torch.cuda.current_stream().synchronize()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Adding torch.cuda.current_stream().synchronize() ensures that all previous asynchronous CUDA operations on the current stream are completed before proceeding. While this guarantees correctness, it can introduce a performance penalty by stalling the CPU, which might be undesirable on a critical path like request handling. If this synchronization is intended to manage dependencies between different CUDA streams, consider using torch.cuda.Event for a more efficient, non-blocking synchronization. If all related operations are on the same stream, this call might be unnecessary due to in-stream ordering guarantees.

Comment on lines +1 to +11
import torch
import numpy as np
from torch.profiler import profile, record_function, ProfilerActivity

torch.cuda.synchronize()
with profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
record_shapes=False,
profile_memory=False,
on_trace_ready=torch.profiler.tensorboard_trace_handler("./log/"),
) as prof:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The torch.profiler.tensorboard_trace_handler will write trace files to the ./log/ directory. If this directory does not exist, the script will fail with a FileNotFoundError. It's good practice to ensure the directory exists before using it. Using a variable for the log directory also improves readability and maintainability.

Suggested change
import torch
import numpy as np
from torch.profiler import profile, record_function, ProfilerActivity
torch.cuda.synchronize()
with profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
record_shapes=False,
profile_memory=False,
on_trace_ready=torch.profiler.tensorboard_trace_handler("./log/"),
) as prof:
import torch
import numpy as np
from torch.profiler import profile, record_function, ProfilerActivity
import os
log_dir = "./log/"
os.makedirs(log_dir, exist_ok=True)
torch.cuda.synchronize()
with profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
record_shapes=False,
profile_memory=False,
on_trace_ready=torch.profiler.tensorboard_trace_handler(log_dir),
) as prof:

@hiworldwzj hiworldwzj changed the title add profile_demo.py and add synchronize in add_reqs function add profile_demo.py and add synchronize in infer_loop Oct 31, 2025
@hiworldwzj hiworldwzj merged commit de8dc64 into main Oct 31, 2025
1 check passed
@hiworldwzj hiworldwzj deleted the wzj branch October 31, 2025 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants